Public Class TextWriterTraceListener
    Inherits Diagnostics.TextWriterTraceListener

#Region " Field "
    Const CST_DATELINE_FORMAT As String = " ******** {0} ******** "
    Private _logTime As Boolean
#End Region

#Region " New "
    ''' <summary>
    ''' Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with System.IO.TextWriter as the output recipient.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        MyBase.New()
    End Sub
    ''' <summary>
    ''' Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class, using the stream as the recipient of the debugging and tracing output.
    ''' </summary>
    ''' <param name="stream">A System.IO.Stream that represents the stream the System.Diagnostics.TextWriterTraceListener writes to.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal stream As System.IO.Stream)
        MyBase.New(stream)
    End Sub
    ''' <summary>
    ''' Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with the specified name, using the stream as the recipient of the debugging and tracing output.
    ''' </summary>
    ''' <param name="stream">A System.IO.Stream that represents the stream the System.Diagnostics.TextWriterTraceListener writes to.</param>
    ''' <param name="name">The name of the new instance.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal stream As System.IO.Stream, ByVal name As String)
        MyBase.New(stream, name)
    End Sub
    ''' <summary>
    ''' Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class using the specified writer as recipient of the tracing or debugging output.
    ''' </summary>
    ''' <param name="writer">A System.IO.TextWriter that receives the output from the System.Diagnostics.TextWriterTraceListener.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal writer As System.IO.TextWriter)
        MyBase.new(writer)
    End Sub
    ''' <summary>
    ''' Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with the specified name, using the specified writer as recipient of the tracing or debugging output.
    ''' </summary>
    ''' <param name="writer">A System.IO.TextWriter that receives the output from the System.Diagnostics.TextWriterTraceListener.</param>
    ''' <param name="name">The name of the new instance.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal writer As System.IO.TextWriter, ByVal name As String)
        MyBase.New(writer, name)
    End Sub
    ''' <summary>
    ''' Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class, using the file as the recipient of the debugging and tracing output.
    ''' </summary>
    ''' <param name="fileName">The name of the file the System.Diagnostics.TextWriterTraceListener writes to.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal fileName As String)
        MyBase.New(fileName)
    End Sub
    ''' <summary>
    ''' Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with the specified name, using the file as the recipient of the debugging and tracing output.
    ''' </summary>
    ''' <param name="fileName">The name of the file the System.Diagnostics.TextWriterTraceListener writes to.</param>
    ''' <param name="name">The name of the new instance.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal fileName As String, ByVal name As String)
        MyBase.New(fileName, name)
    End Sub
    ''' <summary>
    ''' Initializes a new instance of the System.Diagnostics.TextWriterTraceListener class with the specified name, using the file as the recipient of the debugging and tracing output.
    ''' </summary>
    ''' <param name="fileName">The name of the file the System.Diagnostics.TextWriterTraceListener writes to.</param>
    ''' <param name="name">The name of the new instance.</param>
    ''' <param name="logTime">True if this listener is to write date information.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal fileName As String, ByVal name As String, ByVal logTime As Boolean)
        Me.New(fileName, name)
        Me.LogTime = logTime
    End Sub
#End Region

#Region " Properties "
    ''' <summary>
    ''' Gets or sets if the Trace Listener will write the time in the destination.
    ''' </summary>
    ''' <value></value>
    ''' <returns>True if the listener will write the time; false otherwise</returns>
    ''' <remarks></remarks>
    Public Property LogTime() As Boolean
        Get
            Return Me._logTime
        End Get
        Set(ByVal Value As Boolean)
            Me._logTime = Value
        End Set
    End Property
#End Region

#Region " Methods "
    ''' <summary>
    ''' Writes a category name and a message to the listener you create when you implement the System.Diagnostics.TraceListener class, 
    ''' followed by a line terminator.
    ''' </summary>
    ''' <param name="message">A message to write.</param>
    ''' <remarks>If LogTime = True then a line with the time information is also added.</remarks>
    Public Overloads Overrides Sub WriteLine(ByVal message As String)
        Me.ConditionalWriteTime()
        MyBase.WriteLine(message)
    End Sub

    ''' <summary>
    ''' Writes a category name and a message to the listener you create when you implement the System.Diagnostics.TraceListener class, 
    ''' followed by a line terminator.
    ''' </summary>
    ''' <param name="message">A message to write.</param>
    ''' <param name="category">A category name used to organize the output.</param>
    ''' <remarks>If LogTime = True then a line with the time information is also added.</remarks>
    Public Overloads Overrides Sub WriteLine(ByVal message As String, ByVal category As String)
        Me.ConditionalWriteTime()
        MyBase.WriteLine(message, category)
    End Sub

    ''' <summary>
    ''' Writes a line with the current date information if date infor is different than last call.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub ConditionalWriteTime()
        Static lastCallTime As Date
        Dim currentTime As Date = GetNowMinusMilliseconds()
        'MyBase.WriteLine(currentTime)
        If Not lastCallTime.Equals(currentTime) Then
            lastCallTime = currentTime
            If Me.LogTime Then MyBase.WriteLine(String.Format(CST_DATELINE_FORMAT, currentTime))
        End If
    End Sub

    ''' <summary>
    ''' Gets the current Date and Time excluding the milliseconds.
    ''' </summary>
    ''' <returns>The current Date and Time excluding the milliseconds.</returns>
    ''' <remarks></remarks>
    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")> _
    Protected Shared Function GetNowMinusMilliseconds() As Date
        With Date.Now
            Return .AddMilliseconds(-.Millisecond)
        End With
    End Function
#End Region
End Class
